package Listeners;

import RuntimeInfo.State;
import AppView.UtilSection;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class UtilListeners {
    public static UtilSection util;

    // 永久性使用的Listener使用函数返回
    public static ActionListener getDrawLineListener() {
        return e -> {
            State.loseFocus();// 首先清空当前聚焦
            State.cursorState = State.CREATE;// 之后进入绘图模式
            State.curType = State.LINE;
            State.canCreate = true;
        };
    }

    public static ActionListener getDrawRecListener() {
        return e -> {
            State.loseFocus();// 首先清空当前聚焦
            State.cursorState = State.CREATE;
            State.curType = State.RECTANGLE;
            State.canCreate = true;
        };
    }


    public static ActionListener getDrawEllipseListener() {
        return e -> {
            State.loseFocus();// 首先清空当前聚焦
            State.cursorState = State.CREATE;
            State.curType = State.ELLIPSE;
            State.canCreate = true;
        };
    }

    public static ActionListener getDrawTextListener() {
        return e -> {
            State.loseFocus();// 首先清空当前聚焦
            String ret = JOptionPane.showInputDialog(null, null);
            if (ret != null) {
                if (ret.length() == 0) {
                    JOptionPane.showOptionDialog(null, "输入文本不能为空", "错误",
                            JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
                            null, null, null);
                } else {
                    State.cursorState = State.CREATE;
                    State.curType = State.TEXT;
                    State.text = ret;
                    State.canCreate = true;
                }

            }


        };
    }

    public static ActionListener getIncrWeightListener() {
        return e -> {
            if (State.current == null) return;
            State.current.setStrokeWidth(State.current.getStrokeWidth() + 1);
            State.canvas.repaint();
        };
    }

    public static ActionListener getDecrWeightListener() {
        return e -> {
            if (State.current == null) return;
            if (State.current.getStrokeWidth() > 1) {
                State.current.setStrokeWidth(State.current.getStrokeWidth() - 1);
            }
            State.canvas.repaint();
        };
    }

    public static ActionListener getDeleteShapeListener() {
        return e -> {
            if (State.current != null) {
                State.shapeList.remove(State.current);
                State.loseFocus();
                State.canvas.repaint();
            }
        };
    }

    /**
     * 返回选取更多颜色的监听器
     */
    public static ActionListener getSelectColorListener() {
        return e -> util.setCurColor(JColorChooser.showDialog(null, "取色器", null, false));
    }

    public static MouseListener getDefaultColorSectionListner(JPanel sec){
        return new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mouseClicked(e);
                util.setCurColor(sec.getBackground());
            }
        };
    }


}
